package tas.report;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

@WebServlet(name = "ServletTrabajadoresExc", urlPatterns = {"/view/admin/TrabajadoresExc"})
public class ServletMatenimiento_FlagExc extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String autorizacion = (String) request.getSession().getAttribute("autorizacion");
        String ID = (String) request.getSession().getAttribute("ID");

        if ((ID == null) || (autorizacion == null)
                || (!autorizacion.equals("ADMIN"))
                || (!ID.equals(request.getSession().getId()))) {

            response.sendRedirect("../");

        } else {

            request.setCharacterEncoding("UTF-8");
            String fpath = request.getServletContext().getRealPath("") + "/excel/mantenimiento_flag.xlsx";

            try {
                InputStream is = new FileInputStream(fpath);
                Workbook wb = WorkbookFactory.create(is);
                Sheet sheet = wb.getSheetAt(0);

                String data = request.getParameter("data_excel");
                //
                String anio_excel = request.getParameter("anio_excel");
                String ueje_excel = request.getParameter("ueje_excel");
                String corte_excel = request.getParameter("corte_excel");
                String dependencia_excel = request.getParameter("dependencia_excel");
                String area_excel = request.getParameter("area_excel");
                String sede_excel = request.getParameter("sede_excel");
                String estadotrab_excel = request.getParameter("estadotrab_excel");
                
                String numdoc_excel = request.getParameter("numdoc_excel");
                String appaterno_excel = request.getParameter("appaterno_excel");
                String apmaterno_excel = request.getParameter("apmaterno_excel");
                String nombres_excel = request.getParameter("nombres_excel");
                String reglab_excel = request.getParameter("reglab_excel");
                String exoneracion_excel = request.getParameter("exoneracion_excel");
                
                sheet.getRow(3).createCell(3).setCellValue(anio_excel);
                sheet.getRow(4).createCell(3).setCellValue(ueje_excel);
                sheet.getRow(5).createCell(3).setCellValue(corte_excel);
                sheet.getRow(6).createCell(3).setCellValue(dependencia_excel);
                sheet.getRow(7).createCell(3).setCellValue(area_excel);
                sheet.getRow(8).createCell(3).setCellValue(sede_excel);
                sheet.getRow(9).createCell(3).setCellValue(estadotrab_excel);
                
                sheet.getRow(3).createCell(5).setCellValue(numdoc_excel);
                sheet.getRow(4).createCell(5).setCellValue(appaterno_excel);
                sheet.getRow(5).createCell(5).setCellValue(apmaterno_excel);
                sheet.getRow(6).createCell(5).setCellValue(nombres_excel);
                sheet.getRow(7).createCell(5).setCellValue(reglab_excel);
                sheet.getRow(8).createCell(5).setCellValue(exoneracion_excel);
                //

                String[] dato = data.split(";");
                int fil = 10;
                int col = 0;
                Row row = null;
                for (String d : dato) {
                    if ((col % 7) == 0) {
                        row = sheet.createRow(2 + fil);
                        col = 0;
                        ++fil;
                    }

                    if (row != null) {
                        row.createCell(col + 1).setCellValue(d);
                    }

                    ++col;
                }

                response.setContentType(
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

                OutputStream out = response.getOutputStream();
                wb.write(out);
                out.close();

            } catch (InvalidFormatException ex) {
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
